/*
** Title: Dialogs for ICMS 
** Author: LU Yi Chen
** Date: Nov 2013
*/
$(document).ready(function() {

	/*
	**Dialogs on Case Details page
	*/
	$( "#sendConfirmationDialog" ).dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		height:160,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$.jGrowl("Joint invitations are sent out!",{life:5000});
			}
		}
	});



	$('#btnConfirmationDialogAccept').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		height:160,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$.jGrowl("You have accepted this case!",{life:5000});
				$('#acknowledgementDiv').hide('fast');
				$('#btnAcceptDiv').hide('fade', function() {
					$('#btnEditDiv').show('fade');
				});
			}
		}
	});


	$('#btnConfirmationDialogReject').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		height:160,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$.jGrowl("You have rejected this case!",{life:5000});
				$('#acknowledgementDiv').hide('fast');
				$('#btnAcceptDiv').hide('fast');
			}
		}
	});


	/*
	**Dialogs on Update Case page
	*/

	$( "#removeConfirmation" ).dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		height:160,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$.jGrowl("Linked case is deleted!",{life:5000});
			}
		}
	});



	/*
	**Dialogs on Joint resolution page
	*/
	$('#btnJointConfirmationDialogAccept').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		height:160,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$.jGrowl("You have accepted this joint resolution case!",{life:5000});
                
                $('#btnAcceptDiv').hide('fade', function() {
                    $('#DummyResponseTime').remove();
                    $('#responseStatusTD').text('Accepted').removeClass('involveStatus-yellow').addClass('involveStatus-green');
                    var currTime = new Date();
                    var formatedCurrTime = currTime.getHours()+":"+(currTime.getMinutes()<10?"0":"")+currTime.getMinutes();
                    $('#responseTimeTD').append($.datepicker.formatDate("d-M-yy",new Date())+" "+formatedCurrTime);
                    $('#dueDateTD').append($('#responseDatepicker').val());
                });
				$('#acknowledgementDiv').hide('fast');
			}
		}
	});


	$('#btnJointConfirmationDialogReject').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		height:160,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$.jGrowl("You have rejected this Joint Resolution case!",{life:5000});
				$('#acknowledgementDiv').hide('fast');
			}

		}
	});


	$('#timeSelectorDialog').dialog({
        dialogClass: "no-close",
        closeOnEscape:false,
        autoOpen: false,
        resizable: false,
        width:400,
        modal: true,
        buttons: {
            OK: function() {
                $( this ).dialog( "close" );
                $('#responseDatepicker').focus();
            }
        }
    });

	
	$('#masterDetailDialog').dialog({
		dialogClass: "no-close",
		show: {
            effect: "fold",
            duration: 500
        },
        hide: {
            effect: "fold",
            duration: 500
        },
		autoOpen: false,
		height: 750,
		width: 800,
		modal: true,
		closeOnEscape:true,
		buttons: {
			OK: function() {
				$( this ).dialog( "close" );
			}
		}
	});
	$('span.masterCaseIDLink').click(function(event) {
		$('#masterDetailDialog').dialog('open');
	});



	$('#emptyCommentsDialog').dialog({
		dialogClass: "no-close",
		autoOpen: false,
		width: 400,
		modal: true,
		closeOnEscape:true,
		buttons: {
			OK: function() {
				$( this ).dialog( "close" );
				$('#rejectComments').focus();
			}
		}
	})

	$('#selectLeadAgencyDialog').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		width:400,
		modal: true,
		buttons: {
			Cancel: function() {
				// $('#ddlSplitLeadAgency').empty();
				$( this ).dialog( "close" );
			},
			OK: function() {
				$('#splidDialogLeadAgency').text($('#ddlSplitLeadAgency').val());
				$('#ddlSplitLeadAgency').empty();
				// $( this ).dialog( "close" );
			}
		}
	});

	$('#executeDelinkDialog').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		width:400,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				//$('#manageAgenciesOptions').find('input[type="radio"]').checked=false;
				$.jGrowl("Delink action completed!",{life:5000});
				$('#involvedAgenciesTable > tbody > tr').each(function(index, el) {

					var selectedAgency = $(this).find('td:first-child').find('input[type=checkbox]').is(':checked') ? $(this).find('td').eq(1).text() : "";

					if(selectedAgency !== ""){
						$('#ddlSplitLeadAgency').append('<option>'+selectedAgency+'</option>');
					}
				});
				$('#involvedAgenciesTable > tbody > tr > td:first-child').find('input[type=checkbox]').each(function(index, el) {
					if($(this).is(':checked')){
						$(this).parents('tr').remove();
					}
				});
			}
		}
	});


	$('#executeSplitDialog').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		width:400,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$('#involvedAgenciesTable > tbody > tr > td:first-child').find('input[type=checkbox]').each(function(index, el) {
					if($(this).is(':checked')){
						$(this).parents('tr').remove();
					}
				});
				$.jGrowl("Split action completed!",{life:5000});
			}
		}
	});


	$('#executeChangeLeadDialog').dialog({
		dialogClass: "no-close",
		closeOnEscape:false,
		autoOpen: false,
		show: {
			effect: "fold",
			duration: 500
		},
		hide: {
			effect: "fold",
			duration: 500
		},
		resizable: false,
		width:400,
		modal: true,
		buttons: {
			Cancel: function() {
				$( this ).dialog( "close" );
			},
			Yes: function() {
				$( this ).dialog( "close" );
				$('#involvedAgenciesTable > tbody > tr > td:first-child').find('input[type=radio]').each(function(index, el) {
					$('.notAddCbx').find('td:eq(1)').text('NEA');
					if($(this).is(':checked')){
						$(this).parents('tr').find('td:eq(1)').append("(Lead)");
					}
				});
				$.jGrowl("Lead Agency Changed!",{life:5000});
			}
		}
	});


});